<?php

namespace app\bh\controller;

use app\bh\model\DataModel;
use app\bh\model\QuyuModel;
use app\bh\model\RoleModel;
use app\bh\model\UserModel;
use think\Controller;
use think\Db;
use think\Request;

class UserController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->get();
        $pageindex = array_key_exists('index', $data) ? $data['index'] : 1;
        $size = array_key_exists('size', $data) ? $data['size'] : 10;
//        $users = UserModel::select();

        //先查询下面的所有区域级别
//        $users = Db::table('bh_quyu')
//            ->alias('q')
//            ->where('q.fid', 'like', "$quyufid%")
//            ->join('bh_user u', 'u.qyid = q.id')
//            ->where("length(q.fid) > $len ")
//            ->limit(($pageindex - 1) * $size, $size)
//            ->select();
//        $count = Db::table('bh_quyu')
//            ->alias('q')
//            ->where('q.fid', 'like', "$quyufid%")
//            ->join('bh_user u', 'u.qyid = q.id')
//            ->where("length(q.fid) > $len ")
//            ->count();
        $users = UserModel::limit(($pageindex - 1) * $size, $size)->whereNotLike('username', 'root')->select();
        foreach ($users as $v) {
            $quyu = QuyuModel::find($v->qyid);
            $role = RoleModel::find($v->roles);
            $v['quyu'] = $quyu ? $quyu->name : '暂无区域';
            $v['quyuobj'] = $quyu;
            $v['role'] = $role ? $role->name : '暂无角色';
        };
        $count = UserModel::count();
        $data = [
            data => $users,
            count => $count
        ];
        return $this->SuccessReturn('ok', $data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $user_model = new UserModel();
        //
        $data = $request->post();
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $username = array_key_exists('username', $data) ? $data['username'] : '';
        $password = array_key_exists('password', $data) ? $data['password'] : '';
        $qyid = array_key_exists('qyid', $data) ? $data['qyid'] : 0;

        $roles = array_key_exists('roles', $data) ? $data['roles'] : '';
        if ($username == '' || $password == '' || $name == '') {
            return $this->ErrorReturn('400', '缺少必要参数');
        }
        //检查是否存在相同的用户名
        $issetuser = $user_model->where('username', $username)->find();
        if ($issetuser) return $this->ErrorReturn('203', '已存在相同的用户名，请重新创建');

        $new_user = new UserModel();
        $new_user->username = $username;
        $new_user->password = $password;
        $new_user->name = $name;
        $new_user->qyid = $qyid;
        $new_user->roles = $roles;
        $new_user->save();
        $role = RoleModel::find($roles);
        $new_user->role = $role ? $role->name : '暂无角色';
        $quyu = QuyuModel::find($qyid);
        $new_user->quyu = $quyu ? $quyu->name : '暂无区域';
        return $this->SuccessReturn('添加成功', $new_user);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->put();
        $id = array_key_exists('id', $data) ? $data['id'] : 0;
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $username = array_key_exists('username', $data) ? $data['username'] : '';
        $password = array_key_exists('password', $data) ? $data['password'] : '';
        $roles = array_key_exists('roles', $data) ? $data['roles'] : '';
        $qyid = array_key_exists('qyid', $data) ? $data['qyid'] : '';
        if ($id == 0) return $this->ErrorReturn(400, '缺少用户id');
        //验证是否存在用户
        $user = UserModel::where('id', $id)->find();
        if (!$user) return $this->ErrorReturn(400, '不存在用户');

        //改变用户
        if ($name != '') $user->name = $name;
        if ($username != '') {
            //插找是否有相同的用户名
            $issetuser = UserModel::where('username', $username)->find();
            if ($issetuser & $issetuser->id != $id) return $this->ErrorReturn(300, '存在相同的用户名 请重新修改');
            $user->username = $username;
        }

        if ($password != '') $user->password = $password;
        if ($qyid != '') {
            $qyid = $qyid[0];
            $user->qyid = $qyid;
        }
        if ($roles != '') $user->roles = $roles;
        $res = $user->save();
        $role = RoleModel::find($roles);
        $user->role = $role ? $role->name : '暂无角色';
        $quyu = QuyuModel::find($qyid);
        $user->quyu = $quyu ? $quyu->name : '暂无区域';
        $user->quyuobj = $quyu;
        if ($res) return $this->SuccessReturn('修改成功', $user);
        return $this->ErrorReturn(500, '服务器错误 修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->delete('id');
        if ($id == 0) return $this->ErrorReturn(400, '用户id无效');
        //查询是否存在此用户
        $user = UserModel::where('id', $id)->find();

        if (!$user) return $this->ErrorReturn(404, '找不到此用户');
//        $data = DataModel::where('uid', $user['id'])->find();
//        if ($data) return $this->ErrorReturn(400, '当前用户存在数据 请先处理数据');
        $user->delete();
        return $this->SuccessReturn('删除成功');
    }
}
